<template>
  <div class="app-container">
    <v-row :gutter="20">
      <v-col>
        <!-- 搜索表单 -->
        <v-form ref="queryForm" label-width="68px">
          <v-row>
            <v-col md="3" style="padding-bottom: 2px">
              <v-text-field
                label="登录地址"
                v-model="queryParams.ipaddr"
                placeholder="请输入登录地址"
                outlined
                dense
                clearable
                style="width: 250px;"
                @keyup.enter.native="handleQuery"
              ></v-text-field>
            </v-col>
            <v-col md="3" style="padding-bottom: 2px">
              <v-text-field
                label="用户名称"
                v-model="queryParams.userName"
                placeholder="请输入用户名称"
                outlined
                dense
                @keyup.enter.native="handleQuery"
                style="width: 250px;"
              ></v-text-field>
            </v-col>

            <!-- 按钮区-->
            <v-col cols="12"
                   sm="6"
                   md="2" style="padding-bottom: 0;margin-left: 1rem;height: 40px;margin-top: 8px">
              <p style="display: inline-block;position: absolute">
                <v-btn
                  small color="primary"
                  :loading="loading"
                  @click="handleQuery">
                  <v-icon left>
                    mdi-magnify
                  </v-icon>
                  搜索
                </v-btn>
                <v-btn
                  small
                  :loading="loading"
                  @click="resetQuery"
                  style="margin-left: 10px"
                >
                  <v-icon left>
                    mdi-autorenew
                  </v-icon>
                  重置
                </v-btn>
              </p>
            </v-col>
          </v-row>
        </v-form>
      </v-col>
    </v-row>

    <!-- 表格-->
    <v-main>
      <v-data-table v-if="list"
                    :headers="headers"
                    :items="list"
                    :items-per-page="queryParams.pageSize"
                    hide-default-footer
                    :loading="loading"
                    loading-text="加载中,请稍后.."
                    item-key="tokenId"
                    class="elevation-1"
                    no-data-text="暂无数据"
      >

        <!-- 序号 -->
        <template v-slot:item.index="{ item, index }">
          {{ index + 1}}
        </template>
        <!-- 登录时间 -->
        <template v-slot:item.loginTime="{ item }">
          {{ parseTime(item.loginTime) }}
        </template>

        <!-- 按钮插槽 -->
        <template v-slot:item.actions="{ item }">
          <span v-if="item && !item.showConfirmForceLogout">
            <v-btn color="#C62828"
                   small
                   plain
                   height="20"
                   width="40"
                   @click="clickForceLogout(item)">
              <v-icon small>
                 mdi-logout
              </v-icon>
              强退
            </v-btn>
          </span>
          <!-- 确认强退 -->
          <span v-else>
            <v-tooltip top>
              <template v-slot:activator="{ on, attrs }">
                <v-btn
                  small color="error"
                  plain
                  height="20"
                  width="40"
                  v-bind="attrs"
                  v-on="on"
                  @click="handleForceLogout(item)"
                  v-hasPermi="['monitor:online:forceLogout']">
                  <v-icon small>
                    mdi-logout
                  </v-icon>
                  确定
                </v-btn>
              </template>
              <span>请再次点击确定强退</span>
            </v-tooltip>
          </span>
        </template>
      </v-data-table>
      <!-- 外部分页-->
      <div class="d-flex align-center justify-end text-center">
        <div class="d-flex" style="margin-top: 3px;font-size: 14px;font-weight: 400">
          <span>每页</span>
          <v-select
            v-model="queryParams.pageSize"
            :items="numbers"
            dense
            @change="getList" style="max-width: 80px;max-height: 20px;margin-top: -4px;text-align: center;">
            <template v-slot:selection="{ item, index }">
                  <span style="width: 100%">
                    {{item}}
                  </span>
            </template>
          </v-select>
          <span>条记录</span>
        </div>
        <v-pagination
          v-model="queryParams.pageNum"
          @input="getList"
          :length="queryParams.totalPages"
          :total-visible="7"
        ></v-pagination>
      </div>
    </v-main>
  </div>
</template>

<script>
import { list, forceLogout } from "@/api/monitor/online";

export default {
  name: "Online",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ipaddr: undefined,
        userName: undefined,
        totalPages: 0, // 总页数
      },
      headers: [
        { text: '序号', value: 'index', align: 'center', sortable: false },
        { text: '会话编号', value: 'tokenId', sortable: false, align: 'center' },
        { text: '登录名称', value: 'userName', sortable: false, align: 'center' },
        { text: '主机', value: 'ipaddr', sortable: false, align: 'center' },
        { text: '登录时间', value: 'loginTime', sortable: false, align: 'center' },
        { text: '操作', value: 'actions', sortable: false, align: 'center' },
      ],
      numbers: [10, 15, 20, 50, 100], // 每页条数选择
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.ids = [];
      this.loading = true;
      list(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.queryParams.totalPages = Math.ceil(this.total / this.queryParams.pageSize);
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.reset(); // 重置
      this.handleQuery();
    },
    clickForceLogout(item) {
      this.$set(item, 'showConfirmForceLogout', true);
      setTimeout(() => {
        this.$set(item, 'showConfirmForceLogout', false);
      }, 5000);
    },
    /** 强退按钮操作 */
    async handleForceLogout(row) {
      // 关闭上一个消息弹窗
      this.$store.commit("snackbar/CLOSE_SNACKBAR");
      await forceLogout(row.tokenId).then(res => {
        if (res) {
          // 使用全局消息提示
          this.$store.dispatch('snackbar/openSnackbar', {
            msg: '强退成功',
            color: 'success'
          })
          this.getList();
        }
      })
    }
  }
};
</script>

